package com.songtech.product.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.common.dictionary.service.SysDictionaryGroupService;
import com.songtech.common.dictionary.service.SysDictionaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.songtech.product.bean.Product;
import com.songtech.product.service.ProductService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.*;
import java.util.*;


@RestController
@RequestMapping("/product")
public class ProductController {

    private final Logger logger = LoggerFactory.getLogger(ProductController.class);


    @Autowired
    private ProductService productService;


    /**
     * 根据厂家 产品 型号查询产品信息
     * @param companyCode
     * @param productCode
     * @param modelNumber
     * @return
     */
    @RequestMapping(value = "/getProduct",method =RequestMethod.GET)
    public JsonResult getProduct(String companyCode,String productCode ,String modelNumber){
        Map<String,String> map=new HashMap<>();
        map.put("companyCode",companyCode);
        map.put("productCode",productCode);
        map.put("modelNumber",modelNumber);
        List<Product> list=null;
        try {
            list= productService.getProduct(map);
        } catch (Exception e) {
            logger.error("根据厂家，产品，型号查询产品信息异常:"+e.getMessage(),e);
            return new JsonResult(list, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(list, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 编辑产品信息
     * @param pro
     * @return
     */
    @RequestMapping(value = "/updateProduct",method = RequestMethod.POST)
    public JsonResult updateProduct(Product pro){
        int i= 0;
        try {
            i = productService.updateByPrimaryKeySelective(pro);
        } catch (Exception e) {
            logger.error("编辑产品信息异常:"+e.getMessage(),e);
            return new JsonResult(i, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(i, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 上传图片
     * @param imgFile
     * @param proId
     * @return
     */
    @RequestMapping(value = "/uploadImage",method = RequestMethod.POST)
    public JsonResult uploadImage(@RequestParam("imgFile") MultipartFile imgFile,
                                  String proId, HttpServletRequest request){
        int count=0;
        Product p=new Product();
        p.setId(Long.parseLong(proId));
        try {
            String oldName = imgFile.getOriginalFilename();
            //取webapp路径下的文件
            String classpath = request.getServletContext().getRealPath("/WEB-INF/asserts/resource/images/product/");
            String fileName = proId+new Date().getTime()+oldName;
            String saveFileName = classpath  + fileName;
            File file1 = new File(saveFileName);
            imgFile.transferTo(file1);
            String imgPath="/resource/images/product/"+fileName;
            p.setProImg(imgPath);
            count=productService.updateByPrimaryKeySelective(p);
        } catch (IOException e) {
            logger.error("上传图片图片异常："+e.getMessage(),e);
        } catch (IllegalStateException e) {
            logger.error("上传图片图片异常："+e.getMessage(),e);
        } catch (Exception e) {
            logger.error("上传图片图片异常："+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

}
